/*
 * Layer.java
 *
 * Created on January 9, 2008, 7:46 PM
 */

package picto.core;

import java.awt.AlphaComposite;
import java.awt.Composite;
import java.awt.Graphics2D;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import picto.io.Archivable;
import picto.io.ArchiveException;
import picto.io.ArchiveReader;
import picto.io.ArchiveWriter;

/**
 *
 * @author davedes
 */
public class Layer implements Archivable {
    
    private String name;
    private boolean visible = true;
    LayerGroup group = null;
    boolean selectedFlag = true;
    private float opacity = 1.0f;
    private ArchivableImage image = null;
    private Overlay overlay = null;
    
    /** Creates a new instance of Layer */
    public Layer() {
    }
        
    public Layer(String name) {
        this.name = name;
    }
    
    public void paint(Graphics2D g2d) {
        if (image!=null && image.getImage()!=null && isVisible() && getOpacity()>0f) {
            Composite c = g2d.getComposite();
            if (getOpacity()!=1f) 
                g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity));
            g2d.drawImage(image.getImage(), 0, 0, null);
            if (overlay!=null)
                overlay.paintOverlay(g2d);
            g2d.setComposite(c);
        }
    }
    
    public Element write(ArchiveWriter writer) throws ArchiveException {
        Document doc = writer.getDocument();
        Element root = doc.createElement("layer");
        
        if (name!=null)
            root.setAttribute("name", name);
        
        boolean vis = isVisible();
        if (!vis)
            root.setAttribute("visible", Boolean.toString(vis));
        
        if (opacity!=1f)
            root.setAttribute("opacity", Float.toString(opacity));
        
        if (image!=null) {
            root.appendChild(image.write(writer));
        }
        return root;
    }

    public void read(ArchiveReader reader, Element root) throws ArchiveException {
        if (!"layer".equals(root.getNodeName()))
            throw new IllegalArgumentException("Layer must be parsed from <layer> tag");
                
        String nameStr = root.getAttribute("name");
        name = nameStr.length()!=0 ? nameStr : null;
        
        String visStr = root.getAttribute("visible");
        visible = visStr.length()!=0 
                        ? Boolean.parseBoolean(visStr)
                        : true;
        
        String opStr = root.getAttribute("opacity");
        try { opacity = Float.parseFloat(opStr); }
        catch (Exception exc) { opacity = 1f; }
        
        try {
            CachedImage t2 = CachedImage.readCachedImage(reader, root, this.name);
            this.image = t2;
        } catch (ArchiveException exc) {
            System.err.println("WARN: Cannot load image data in layer "+this.name);
        }
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public LayerGroup getGroup() {
        return group;
    }
    
    public String toString() {
        return getName();
    }

    public float getOpacity() {
        return opacity;
    }

    public void setOpacity(float opacity) {
        this.opacity = opacity;
    }

    public ArchivableImage getImage() {
        return image;
    }

    public void setImage(ArchivableImage image) {
        this.image = image;
    }

    public Overlay getOverlay() {
        return overlay;
    }

    public void setOverlay(Overlay overlay) {
        this.overlay = overlay;
    }
}
